package com.lcp.controller;

import com.lcp.common.Result;
import com.lcp.entity.DietRecord;
import com.lcp.entity.User;
import com.lcp.service.DietRecordService;
import com.lcp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/diet-records")
public class DietRecordController {

    @Autowired
    private DietRecordService dietRecordService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result<DietRecord> getById(@PathVariable Long id) {
        return Result.success(dietRecordService.getById(id));
    }

    @GetMapping("/current")
    public Result<List<DietRecord>> getCurrentUserRecords() {
        User currentUser = userService.getCurrentUser();
        return Result.success(dietRecordService.listByUserId(currentUser.getId()));
    }

    @PostMapping
    public Result<DietRecord> create(@RequestBody DietRecord record) {
        User currentUser = userService.getCurrentUser();
        record.setUserId(currentUser.getId());
        return Result.success(dietRecordService.create(record));
    }

    @PutMapping("/{id}")
    public Result<DietRecord> update(@PathVariable Long id, @RequestBody DietRecord record) {
        record.setId(id);
        return Result.success(dietRecordService.update(record));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        dietRecordService.delete(id);
        return Result.success();
    }

    @GetMapping("/date-range")
    public Result<List<DietRecord>> listByDateRange(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        User currentUser = userService.getCurrentUser();
        return Result.success(
            dietRecordService.listByDateRange(currentUser.getId(), startDate, endDate)
        );
    }

    @GetMapping("/meal-type")
    public Result<List<DietRecord>> listByMealType(
            @RequestParam String mealType,
            @RequestParam String date) {
        User currentUser = userService.getCurrentUser();
        return Result.success(
            dietRecordService.listByMealType(currentUser.getId(), mealType, date)
        );
    }

    @GetMapping("/daily-summary")
    public Result<Map<String, Object>> getDailyNutritionSummary(@RequestParam String date) {
        User currentUser = userService.getCurrentUser();
        return Result.success(
            dietRecordService.getDailyNutritionSummary(currentUser.getId(), date)
        );
    }

    @GetMapping("/weekly-trend")
    public Result<Map<String, Object>> getWeeklyNutritionTrend(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        User currentUser = userService.getCurrentUser();
        return Result.success(
            dietRecordService.getWeeklyNutritionTrend(currentUser.getId(), startDate, endDate)
        );
    }
} 